package com.product.system.controller;

import cn.hutool.core.lang.tree.Tree;
import com.alibaba.excel.EasyExcel;
import com.product.system.domain.SysDept;
import com.product.system.service.SysDeptService;
import com.product.system.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("dept")
public class SysDeptController {


    @Autowired
    SysDeptService sysDeptService;

    @RequestMapping("deptList")
    public Result deptList(@RequestBody SysDept sysDept){

        if (!"".equals(sysDept.getDeptName())||!"".equals(sysDept.getStatus())){
            List<SysDept> sysDepts = sysDeptService.getBy(sysDept);
            return Result.success(sysDepts);
        }

        List<Tree<String>> deptList = sysDeptService.deptList();

        return Result.success(deptList);
    }

    @RequestMapping("saveDept")
    public Result saveDept(@RequestBody @Valid SysDept sysDept, BindingResult result){
        if (result.hasErrors()){
            System.out.println(result.hasErrors());
            return Result.fail(result.getAllErrors().get(0).getDefaultMessage());
        }
        sysDeptService.saveDept(sysDept);
        return Result.success();
    }

    @RequestMapping("getParentById")
    public Result getParentById(@RequestParam Integer id){
        SysDept sysDept=sysDeptService.getParentById(id);
        return Result.success(sysDept);
    }

    @RequestMapping("getById")
    public Result getById(@RequestParam Integer id){

        SysDept sysDept=sysDeptService.getById(id);

        return Result.success(sysDept);
    }

    @RequestMapping("getParent")
    public Result getParent(){

        List<Tree<String>> parents=sysDeptService.getParent();

        return Result.success(parents);
    }

    @RequestMapping("delDept")
    public Result delDept(@RequestParam Integer id){

        sysDeptService.delDept(id);

        return Result.success();
    }

    @RequestMapping("updateDept")
    public Result updateDept(@RequestBody SysDept sysDept){
        sysDeptService.updateById(sysDept);
        return Result.success();
    }


    @GetMapping("download")
    public void  download(HttpServletResponse response) throws Exception {
        String fileName = URLEncoder.encode("部门管理", "UTF-8").replaceAll("\\+","%20");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<SysDept> deptList = sysDeptService.getDeptList();
        EasyExcel.write(response.getOutputStream(), SysDept.class).sheet("部门信息").doWrite(deptList);

    }

}
